package com.gopay.scheduler.job;

import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.cbrmb.CbRmbIntlDishonorAccountingFailService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author b.zou
 * @description: 拒付或调单订单，若扣款（冻结）失败，跑批也没有扣款（冻结）成功，则每七天发邮件通知风控，订单不重复通知
 * @class CbRmbIntlDishonorAccountingFailRiskEmailJob
 * @date 2018.1.8
 */
public class CbRmbIntlDishonorAccountingFailRCEmailJob extends GopayBaseJob {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "cbRmbIntlDishonorAccountingFailService")
    private CbRmbIntlDishonorAccountingFailService cbRmbIntlDishonorAccountingFailService;

    @Override
    protected void doJobInternal() {
        logger.info("======= CbRmbIntlDishonorAccountingFailRCEmailJob - 跨境外卡拒付(调单)订单超过七天扣款失败发送风控邮件定时任务 START=======");

        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -7);
            Date date = c.getTime();
            /* 七天前零点 */
            Date timeBegin = new Date(date.getYear(), date.getMonth(), date.getDate(), 0, 0, 0);
            /* 七天前末点 */
            Date timeEnd = new Date(date.getYear(), date.getMonth(), date.getDate(), 23, 59, 59);
            Map<String, Object> paramsOfCust = new HashMap<>(2);
            paramsOfCust.put("timeBegin", sdf.format(timeBegin));
            paramsOfCust.put("timeEnd", sdf.format(timeEnd));
            /* find custIds */
            List<String> list = cbRmbIntlDishonorAccountingFailService.getCustIds(paramsOfCust);
            StringBuilder stringBuilder;
            if (null != list && list.size() > 0) {
                stringBuilder = new StringBuilder();
                List<String> infoList = new ArrayList<>();
                for (String custId : list) {
                    paramsOfCust.put("custId", custId);
                    String msgInfo = cbRmbIntlDishonorAccountingFailService.getRiskMsgByCustId(paramsOfCust);
                    if (StringUtils.isNotBlank(msgInfo)) {
                        infoList.add(msgInfo);
                    }
                }
                int i = 1;
                for (String info : infoList) {
                    stringBuilder.append(i + "、" + info);
                    i++;
                }
                String msg = stringBuilder.toString();
                if (StringUtils.isNotBlank(msg)) {
                    cbRmbIntlDishonorAccountingFailService.sendRiskControllerEmail(msg);
                }
            }
        /* test */
        /*paramsOfCust.put("timeBegin","20171208000000");
        paramsOfCust.put("timeEnd","20171209000000");
        List<String> list = new ArrayList<>();
        list.add("0000002589");
        StringBuilder stringBuilder;
        if(null!=list&&list.size()>0){
            stringBuilder = new StringBuilder();
            List<String> infoList = new ArrayList<>();
            for (String custId : list) {
                paramsOfCust.put("custId", custId);
                String msgInfo = cbRmbIntlDishonorAccountingFailService.getRiskMsgByCustId(paramsOfCust);
                if(StringUtils.isNotBlank(msgInfo)){
                    infoList.add(msgInfo);
                }
            }
            int i = 1;
            for (String info : infoList) {
                stringBuilder.append(i+"、"+info);
                i++;
            }
            String msg = stringBuilder.toString();
            if(StringUtils.isNotBlank(msg)){
                cbRmbIntlDishonorAccountingFailService.sendRiskControllerEmail(msg);
            }
        }*/
        } catch (Exception e) {
            logger.error("-------跨境人民币外卡支付180天解冻保证金任务失败，错误信息：{}", e);
        }
        logger.info("======= CbRmbIntlDishonorAccountingFailRCEmailJob - 跨境外卡拒付(调单)订单超过七天扣款失败发送风控邮件定时任务 STOP =======");
    }


}
